package com.turing.post.organization.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.VoucherAdjustOrderDetail;
import com.turing.post.organization.util.VoucherAdjustOrderDetailBusiness;
import com.turing.post.organization.util.VoucherAdjustOrderDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.VoucherAdjustOrderDetailBusinessImpl")
@Remote
@Local
public class VoucherAdjustOrderDetailBusinessImpl implements
		VoucherAdjustOrderDetailBusiness {

	@EJB(mappedName = "organization.impl.VoucherAdjustOrderDetailDaoImpl")
	private VoucherAdjustOrderDetailDao voucherAdjustOrderDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	public void insertVoucherAdjustOrderDetail(PCPSWebInput webInput,
			VoucherAdjustOrderDetail voucherAdjustOrderDetail)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 调整单ID
		if (voucherAdjustOrderDetail.getVoucherAdjustOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证调整单明细中调整单ID为空!");
		}
		// 单内序号
		if (voucherAdjustOrderDetail.getSeqNo() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"单内序号为空!");
		}
		// 凭证种类ID
		if (voucherAdjustOrderDetail.getVoucherId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类ID为空!");
		}
		// 起始凭证号
		if (PCPSUtil.isEmpty(voucherAdjustOrderDetail.getStartVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"起始凭证号为空!");
		}
		// 终止凭证号
		if (PCPSUtil.isEmpty(voucherAdjustOrderDetail.getEndVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"终止凭证号为空!");
		}
		// 前台得到的数量
		BigInteger tempkcount = new BigInteger(voucherAdjustOrderDetail
				.getEndVoucherNo()).subtract(new BigInteger(
				voucherAdjustOrderDetail.getStartVoucherNo()));
		voucherAdjustOrderDetail.setCount(tempkcount.add(BigInteger.ONE).intValue());
		if(voucherAdjustOrderDaoImpl.IsSeqNo(webInput.getOperBranchNo().substring(0,6), voucherAdjustOrderDetail.getStartVoucherNo().trim(), voucherAdjustOrderDetail.getEndVoucherNo().trim(), voucherAdjustOrderDetail.getVoucherId(),webInput.getOperBranchNo())>0){
			  throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", voucherAdjustOrderDetail.getStartVoucherNo().trim()+"----"+ voucherAdjustOrderDetail.getEndVoucherNo().trim()+"号段中已经有调整待审核的卡!或号段不在本机构下!");
		}
		// 插入数据
		voucherAdjustOrderDaoImpl
				.insertVoucherAdjustOrderDetail(voucherAdjustOrderDetail);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "凭证调整单明细新增",
				voucherAdjustOrderDetail, SysOperLog.operType_Add,
				"VoucherAdjustOrderId:"
						+ voucherAdjustOrderDetail.getVoucherAdjustOrderId()
						+ "VoucherId:" + voucherAdjustOrderDetail.getVoucherId());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherAdjustOrderDetail obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
